var imkbizle = function() {

	//private area
	var lastRequest = null;
	var imkbizlePanel = null;
	var toggleRefreshPanel = null;
	var source = isnet; //TODO: allow user to select source site.

	var setLabel = function(stock) {
		imkbizlePanel.label = source.stockname + ": " + stock.Latest;
	};

	var setArrow = function(stock) {
		imkbizlePanel.image = "chrome://imkbizle/skin/arrow_"
			+ ((stock.Latest >= stock.Open) ? "up" : "down") + ".gif";
	};

	var setToolTipText = function(stock) {
		var tiptext = "";
		$H(stock).each(function(stat) {
			if (stat.value) {
				tiptext += source.locale.getString("stock." + stat.key) + ": " + stat.value + " | ";
			}
		});
		imkbizlePanel.tooltipText = tiptext.replace(/(.*) \| $/, "$1"); //strip last seperator
	};

	var infoReceived = function(transport) {
		var stock = source.getStock(transport.responseText);
		setLabel(stock);
		setArrow(stock);
		setToolTipText(stock);
	};

	var resetPanel = function() {
		imkbizlePanel.label = source.locale.getString("status.loading");
		imkbizlePanel.image = "";
		imkbizlePanel.tooltipText = source.locale.getString("status.wait");
	};

	var addMenuItem = function(menu, label) {
		var item = document.createElement("menuitem");
		item.setAttribute("label", label);
		menu.appendChild(item);
	};

	var fillStockList = function() {
		var stockmenu = document.getElementById('stock-menu');
		var stocks = prefs.getStocks();
		stocks.each(function(stock) {
			addMenuItem(stockmenu, stock);
		});
		return stocks;
	};

	// public area
	return {

		refresh_enabled: true,

		// Install a timeout handler to install the interval routine
		startup: function() {
			var stocks = fillStockList();
			imkbizlePanel = document.getElementById('imkbizle');
			toggleRefreshPanel = document.getElementById('toggle-refresh');
			source.locale = document.getElementById("string-bundle");
			// check if any saved value exists for default stock to be
			// shown at startup, if none show first stock.
			var index = prefs.getIntPref(prefs.settings.default_stock.target, 0);
			source.stockname = stocks[index];
			imkbizle.refreshInformation();
			var refresh_interval = prefs.getIntPref(prefs.settings.refresh_interval.target, 2) + 1;
			window.setInterval(imkbizle.refreshInformation, refresh_interval*60*1000); //default 3 min.
		},

		// Called periodically to refresh the stock information
		refreshInformation: function() {
			if (imkbizle.refresh_enabled) {
				resetPanel();
				lastRequest = new Ajax.Request(source.url, {
					method: 'get',
					parameters: source.getParams(),
					onSuccess: infoReceived,
					onFailure: function(){ throw("couldn't retrieve data from '" + source.url + "'") }
				});
			}
		},

		onSelectStock: function(event) {
			source.stockname = event.target.label;
			imkbizle.refreshInformation();
		},

		showSourcePage: function(event) {
			var load_location = prefs.settings.load_location;
			var url = lastRequest.url;
			var target = prefs.getIntPref(load_location.target);
			if (target == prefs.NEW_WINDOW) {
				window.open(url);
			} else if ((target == prefs.NEW_TAB) || (target == prefs.UNFOCUSED_TAB)) {
				var myTab = getBrowser().addTab(url, null, null);
				if(target == prefs.NEW_TAB)
					getBrowser().selectedTab = myTab;
			} else {
				getBrowser().loadURI(url);
			}
		},

		toggleRefresh: function(event) {
			imkbizle.refresh_enabled = !imkbizle.refresh_enabled;
			var label_key = imkbizle.refresh_enabled
				? "label.disable_refresh"
				: "label.enable_refresh";
			toggleRefreshPanel.label = source.locale.getString(label_key);
		},

		openPreferences: function(event) {
			window.openDialog("chrome://imkbizle/content/prefs.xul",
					"_blank", "centerscreen, chrome,resizable=no,dependent=yes")
		}
	}
}()

// Install load handler
window.addEventListener("load", function(e) { imkbizle.startup(); }, false);
